package com.study.talk.adapter;


import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.study.talk.app.base.BaseActivity;
import com.study.talk.databinding.UserListCheckedItemBinding;
import com.study.talk.entity.UserInfo;
import com.study.talk.net.OkHttp;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class UserListCheckedListAdapter extends RecyclerView.Adapter<UserListCheckedListAdapter.ViewHolder>{

    private final List<UserInfo> data;
    private final BaseActivity activity;
    public final Map<Integer,Boolean> selectMap = new HashMap<>();


    public UserListCheckedListAdapter(List<UserInfo> data, BaseActivity activity){
        this.data = data;
        this.activity = activity;
        data.forEach(userInfo -> selectMap.put(userInfo.getId(), false));
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ViewHolder(UserListCheckedItemBinding.inflate(LayoutInflater.from(parent.getContext()),parent,false));
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        UserInfo userInfo = data.get(position);
        Glide.with(activity).load(OkHttp.URL + userInfo.getAvatar()).into(holder.itemBinding.userListCheckedItemHeadImg);
        holder.itemBinding.userListCheckedItemHeadUsername.setText(userInfo.getUsername());
        holder.itemBinding.userListCheckedItemHeadIntroduce.setText(userInfo.getIntroduce());
        holder.itemBinding.userListCheckedItemCheckbox.setOnCheckedChangeListener((compoundButton, b) -> selectMap.put(data.get(position).getId(), b));
    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder{

        UserListCheckedItemBinding itemBinding;

        public ViewHolder(UserListCheckedItemBinding itemBinding) {
            super(itemBinding.getRoot());
            this.itemBinding = itemBinding;
        }
    }


}
